package com.jiuyou.clientController.attachedData;

import com.ruoyi.attached.domain.Attached;
import com.ruoyi.attached.service.IAttachedService;
import com.ruoyi.attachedData.domain.AttachedData;
import com.ruoyi.attachedData.service.IAttachedDataService;
import com.ruoyi.commission.domain.Commission;
import com.ruoyi.commission.service.ICommissionService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.DicConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 附详情Controller
 *
 * @author ruoyi
 * @date 2025-05-09
 */
@RestController
@RequestMapping("/client/attachedData")
public class ClientAttachedDataController extends BaseController {
    @Autowired
    private IAttachedService attachedService;

    @Autowired
    private IAttachedDataService attachedDataService;

    @Autowired
    private ICommissionService commissionService;

    /**
     * 查询附详情列表
     */
    @GetMapping("/listAll")
    public AjaxResult listAll(AttachedData attachedData) {
        List<AttachedData> list = attachedDataService.selectAttachedDataList(attachedData);
        return AjaxResult.success(list);
    }

    /**
     * 查询附详情列表
     */
    @GetMapping("/list")
    public TableDataInfo list(AttachedData attachedData) {
        startPage();
        List<AttachedData> list = attachedDataService.selectAttachedDataList(attachedData);
        return getDataTable(list);
    }

    /**
     * 导出附详情列表
     */
    @Log(title = "附详情", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(AttachedData attachedData) {
        List<AttachedData> list = attachedDataService.selectAttachedDataList(attachedData);
        ExcelUtil<AttachedData> util = new ExcelUtil<AttachedData>(AttachedData.class);
        return util.exportExcel(list, "附详情数据");
    }

    /**
     * 获取附详情详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(attachedDataService.selectAttachedDataById(id));
    }

    /**
     * 新增附详情
     */
    @Log(title = "附详情", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AttachedData attachedData) {
        return toAjax(attachedDataService.insertAttachedData(attachedData));
    }

    /**
     * 修改附详情
     */
    @Log(title = "附详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AttachedData attachedData) {
        attachedData.setUpdateType(DicConstants.BUSINESS_Manager.getCode());
        return toAjax(attachedDataService.updateAttachedData(attachedData));
    }

    /**
     * 删除附详情
     */
    @Log(title = "附详情", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(attachedDataService.logicDeleteAttachedData(ids, DicConstants.BUSINESS_Manager.getCode(), SecurityUtils.getUserId()));
    }


    //    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, Long commissionId,Long type) throws Exception {
        // 获取原始文件名
        String originalFilename = file.getOriginalFilename();
        Commission c_one = commissionService.selectCommissionById(commissionId);

        Attached attached = new Attached();
        attached.setName(originalFilename);
        attached.setCommissionId(commissionId);
        attached.setEngineeringId(c_one.getEngineeringId());
        attached.setEnterpriseId(c_one.getEnterpriseId());
        attachedService.insertAttached(attached);

        Long attachedId = attached.getId();

        ExcelUtil<AttachedData> util = new ExcelUtil<AttachedData>(AttachedData.class);
        List<AttachedData> attachedDataList = util.importExcel(file.getInputStream());
        System.out.println(attachedDataList);
        String message = attachedDataService.importAttachedData(attachedDataList, attachedId,commissionId,c_one.getEngineeringId(),type);
        return AjaxResult.success(message);
    }
}
